/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string>

#include "yaffut.h"
#include "myassert.h"

#include "CommChannel.h"
#include "TestSession.h"
#include "color.h"
#include "Error.h"

using namespace Chronos;
using namespace std;

struct CommChannelTest : public CommChannel {
	CommChannelTest()
	    : CommChannel("gen", ANSI_BLUE)
   		, session1("player1")
   		, session2("player2")
	{
    	addMember(session1);
	}

    TestSession session1;
    TestSession session2;
};


TEST(CommChannelTest, testAddingMember) {
    session1.SESSION_CHECKWRITTEN("You joined the ["ANSI_BLUE"gen"ANSI_NORMAL"] channel.\n");

    addMember(session2);
    session2.clear();
    session1.SESSION_CHECKWRITTEN(ANSI_BLUE"[gen] player2 joins the channel."ANSI_NORMAL"\n");
}


TEST(CommChannelTest, testAddingMemberTwice) {
    session1.clear();
    addMember(session1);
    session1.SESSION_CHECKWRITTEN("You are already member of channel ["ANSI_BLUE"gen"ANSI_NORMAL"].\n");
}


TEST(CommChannelTest, testRemovingMember) {
    addMember(session2);
    session1.clear();
    session2.clear();

    removeMember(session1);
    session1.SESSION_CHECKWRITTEN("You left the ["ANSI_BLUE"gen"ANSI_NORMAL"] channel.\n");
    session2.SESSION_CHECKWRITTEN(ANSI_BLUE"[gen] player1 has left the channel."ANSI_NORMAL"\n");
}


TEST(CommChannelTest, testRemovingNonMember) {
    removeMember(session2);
    session2.SESSION_CHECKWRITTEN("You are not a member of channel ["ANSI_BLUE"gen"ANSI_NORMAL"].\n");
}


TEST(CommChannelTest, testWritingtoChannel) {
    session1.clear();

    write(session2, "hello");
    session2.SESSION_CHECKWRITTEN("You are not a member of channel ["ANSI_BLUE"gen"ANSI_NORMAL"].\n");
    session1.SESSION_CHECKWRITTEN("");

    addMember(session2);
    session2.clear();
    session1.clear();
   
    write(session2, "hello");
    session2.SESSION_CHECKWRITTEN(ANSI_BLUE"[gen] player2: hello"ANSI_NORMAL"\n");
    session1.SESSION_CHECKWRITTEN(ANSI_BLUE"[gen] player2: hello"ANSI_NORMAL"\n");
}


